//
//  CertificateHelper.m
//  AgoraSignal
//
//  Created by WenDaoJiang on 17/5/9.
//  Copyright © 2017年 WenDaoJiang. All rights reserved.
//

#import "CertificateHelper.h"

@implementation CertificateHelper

+ (NSString *)SignalingKeyByAppId: (NSString *) appId Certificate:(NSString *)certificate Account:(NSString*)account ExpiredTime:(unsigned)expiredTime {
    return [[self new] SignalingKeyByAppId:appId Certificate:certificate Account:account ExpiredTime:expiredTime];
}

- (NSString *) SignalingKeyByAppId:(NSString *)_appID Certificate:(NSString *)certificate Account:(NSString*)account ExpiredTime:(unsigned)expiredTime {
    //1:appId:expiredTime:md5(account + appId + appCertificate + expiredTime)
    
    NSString * sign = [self MD5:[NSString stringWithFormat:@"%@%@%@%d", account, _appID, certificate, expiredTime]];
    return [NSString stringWithFormat:@"1:%@:%d:%@", _appID, expiredTime, sign];
}


- (NSString*)MD5:(NSString*)s
{
    // Create pointer to the string as UTF8
    const char *ptr = [s UTF8String];
    
    // Create byte array of unsigned chars
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
    
    // Create 16 byte MD5 hash value, store in buffer
    CC_MD5(ptr, (uint32_t)strlen(ptr), md5Buffer);
    
    // Convert MD5 value in the buffer to NSString of hex values
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x",md5Buffer[i]];
    
    return output;
}


@end
